import {defineStore} from "pinia";
import {h} from "vue";
import {renderIcon} from "@/utils";
import router from "@/router";

export const useDatabaseStore = defineStore('database', {
    persist: true,
    state: () => {
        return {
            databaseList: [] as IDatabase[],
        }
    },
    /**
     * 封装计算属性，有缓存功能
     */
    getters: {
        menuOptions(state) {
            return state.databaseList.map((item: IDatabase) => {
                return {
                    label: () =>
                        h(
                            'div',
                            {
                                class: 'font-bold text-xl',
                                onClick: () => {
                                    router.push({
                                        path: '/info/' + encodeURIComponent(item.name) + '/',
                                    })
                                },
                            },
                            item.label
                        ),
                    icon: item.available ? renderIcon('material-symbols:database') : renderIcon('iconoir:db-error'),
                    key: item.key,
                    children: item.children.map((it: any, index: number) => {
                        return {
                            label: () =>
                                h(
                                    'div',
                                    {
                                        class: 'font-bold text-xl',
                                        onClick: () => {
                                            router.push({
                                                path: '/data/' + encodeURIComponent(item.name) + '/' + encodeURIComponent(index),
                                            })
                                        },
                                    },
                                    it.label
                                ),
                            key: it.key
                        }
                    }),
                    info: item.info,
                    name: item.name
                }
            })
        }
    },
    /**
     * 封装业务逻辑，修改state
     */
    actions: {
        getDd(key: any) {
            return this.databaseList.find(item => item?.key === key);
        },
        pushDb(val: any) {

            const exists = this.databaseList.some(item => item?.key === val.key);
            if (!exists) {
                this.databaseList.push(val);
            } else {
                this.databaseList.map(item => {
                    if (item.key === val.key) {
                        item.available = true
                        item.info = val.info
                    }
                })
            }
        },
        successDb(key: any) {
            this.databaseList.map(item => {
                if (item.key === key) {
                    item.available = true
                }
            })
        },
        errorDb(key: any) {
            this.databaseList.map(item => {
                if (item.key === key) {
                    item.available = false
                    router.push({
                        path: '/info/' + encodeURIComponent(item.name) + '/',
                    })
                }
            })
        },
        removeDb(key: any) {
            const index = this.databaseList.findIndex(item => item?.key === key);
            if (index !== -1) {
                this.databaseList.splice(index, 1);
            }
        }

    },
})
